//
//  ScroingTabel3v3ViewController.m
//  LanQiuQuan
//
//  Created by TonyGenius on 16/4/11.
//  Copyright © 2016年 Rip. All rights reserved.
//

#import "ScroingTabel3v3ViewController.h"
#import "Marco.h"

@interface ScroingTabel3v3ViewController ()
{
    CGPoint _lastPoint;
    CGRect _originRect;
    CGPoint _originCenter;
    float _width;
    float _height;
    float _originWidth;
    float _originHeight;
    
    float _switchHeight;
    float _switchOriginHeight;
    NSInteger _selectedTag;
    NSInteger _selectedPlayerTag;
}


@property (weak, nonatomic) IBOutlet UILabel *time;

@property (weak, nonatomic) IBOutlet UIImageView *homeTeamImage;

@property (weak, nonatomic) IBOutlet UILabel *scoreboardImage;

@property (weak, nonatomic) IBOutlet UIImageView *guestImage;

@property (weak, nonatomic) IBOutlet UILabel *pride;

@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *scroingTableArray;



@end

@implementation ScroingTabel3v3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _selectedTag = 1000;
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸开始");
    UITouch *touch = [touches anyObject];
    CGPoint beginPoint = [touch locationInView:self.view];
    _lastPoint = beginPoint;
    if (touch.view.tag >= 10 && touch.view.tag <=15) {
        _originRect = touch.view.frame;
    }
    //    NSLog(@"%@",touch.view);
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸移动");
    UITouch *touch = [touches anyObject];
    CGPoint movePoint = [touch locationInView:self.view];
    
    if (touch.view.tag >= 10 && touch.view.tag <=15) {
        [self movePlayer:movePoint andTouchView:touch.view];
        
        for (UILabel *label in _scroingTableArray) {
            if (CGRectIntersectsRect(label.frame, touch.view.frame)) {
                
                CGRect rect = CGRectIntersection(label.frame, touch.view.frame);
                
                if (rect.size.width*rect.size.height >= (_originWidth*_originHeight)/2) {
                    
                    if (label.tag == 20) {
                        if (label.frame.size.height != _switchHeight) {
                            
                            [UIView animateWithDuration:0.1 animations:^{
                                
                                CGRect rect = label.frame;
                                rect.size.height = _switchHeight;
                                label.bounds = rect;
                                
                            }];
                            _selectedTag = label.tag;
                            //                            NSLog(@"_selectedTag______%d",label.tag);
                        }
                    }else
                    {
                        if (label.frame.size.width != _width && label.frame.size.height != _height) {
                            [UIView animateWithDuration:0.1 animations:^{
                                
                                CGRect rect = label.frame;
                                rect.size.width = _width;
                                rect.size.height = _height;
                                label.bounds = rect;
                                
                            }];
                            _selectedTag = label.tag;
                            //                            NSLog(@"_selectedTag______%d",label.tag);
                        }
                    }
                }else
                {
                    //将选中置为一个不存在的值
                    _selectedTag = 1000;
                    
                    if (label.tag == 20) {
                        [UIView animateWithDuration:.1 animations:^{
                            CGRect rect = label.frame;
                            rect.size.height = _switchOriginHeight;
                            label.bounds = rect;
                        }];
                    }else
                    {
                        if (label.frame.size.width != _originWidth && label.frame.size.height != _originHeight) {
                            [UIView animateWithDuration:0.1 animations:^{
                                CGRect rect = label.frame;
                                rect.size.width = _originWidth;
                                rect.size.height = _originHeight;
                                
                                label.bounds = rect;
                            }];
                            
                        }
                    }
                }
                //                NSLog(@"%@",NSStringFromCGRect(rect));
            }
            else{
                if (label.tag == 20) {
                    if (label.frame.size.height != _switchOriginHeight) {
                        [UIView animateWithDuration:0.1 animations:^{
                            CGRect rect = label.frame;
                            rect.size.height = _switchOriginHeight;
                            label.bounds = rect;
                        }];
                    }
                }else{
                    if (label.frame.size.width != _originWidth && label.frame.size.height != _originHeight) {
                        [UIView animateWithDuration:0.1 animations:^{
                            CGRect rect = label.frame;
                            rect.size.width = _originWidth;
                            rect.size.height = _originHeight;
                            label.bounds = rect;
                        }];
                    }
                }
            }
        }
        
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
    UITouch *touch = [touches anyObject];
    if (touch.view.tag >=10 && touch.view.tag <= 15) {
        
        _selectedPlayerTag = touch.view.tag;
        [UIView animateWithDuration:0.5 animations:^{
            touch.view.frame = _originRect;
        }];
        
        if (_selectedTag>=20 && _selectedTag<=33) {
//            UILabel *label = [self.view viewWithTag:_selectedTag];
            
            switch (_selectedTag) {
                case 20://换人
                {
                    
                }
                    break;
                case 21://失误
                {
                    
                }
                    break;
                case 22://篮板
                {
                    
                }
                    break;
                case 23://抢断
                {
                    
                }
                    break;
                case 24://盖帽
                {
                    
                }
                    break;
                case 25://助攻
                {
                    
                }
                    break;
                case 26://个人犯规
                {
                    
                }
                    break;
                case 27://罚球
                {
                    
                }
                    break;
                case 28://罚球不中
                {
                    
                }
                    break;
                case 29://两分
                {
                    
                }
                    break;
                case 30://两分不中
                {
                    
                }
                    break;
                case 31://三分
                {
                    
                }
                    break;
                case 32://三分不中
                {
                    
                }
                    break;
                case 33://技术犯规
                {
                    
                }
                    break;
                default:
                    break;
            }
        }
        
        
    }
}

- (void)movePlayer:(CGPoint)currentPoint andTouchView:(UIView *)view
{
    float playerPointX = currentPoint.x - _lastPoint.x;
    float playerPointY = currentPoint.y - _lastPoint.y;
    
    float newPointX = view.frame.origin.x + playerPointX;
    float newPointY = view.frame.origin.y + playerPointY;
    
    if (newPointX < 0) {
        newPointX = 0;
    }
    if (newPointX >kSCREENWIDTH - view.bounds.size.width) {
        newPointX = kSCREENWIDTH - view.bounds.size.width;
    }
    if (newPointY < 0) {
        newPointY = 0;
    }
    if (newPointY > kSCREENHEIGHT) {
        newPointY = kSCREENHEIGHT;
    }
    view.frame = CGRectMake(newPointX, newPointY, view.bounds.size.width, view.bounds.size.height);
    _lastPoint = currentPoint;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
